<?php

namespace register\view;

require_once("./common/Filter.php");
require_once("./register/model/RegisterObserver.php");
require_once("./login/model/UserName.php");
require_once("./login/model/Password.php");

class RegisterView implements \register\model\RegisterObserver {

	//positions in input arrays
	private static $REGISTER = "register";
	private static $USERNAME = "LoginView::UserName";
	private static $PASSWORD = "LoginView::Password";
	private static $REPEATPASSWORD = "LoginView::RepeatPassword";
	private static $SUBMIT = "LoginView::Submit";

	private $LoginView;

	private $registstrationOK = false;

	/**
	 * Used to pass messages to client
	 * @var string
	 */
	private $message = "";

	/**
	 *
	 * @param loginviewLoginView $loginView
	 */
	public function __construct(\login\view\LoginView $loginView) {
		$this->LoginView = $loginView;
	}
		
	/**
	 * @return String HTML
	 */
	public function getRegisterBox() {
		$user =\Common\Filter::sanitizeString($this->getUserName());	
		$html = "<a href='./'>Tillbaka</a>
			<form action='?".self::$REGISTER."' method='post' enctype='multipart/form-data'>
				<fieldset>
					$this->message
					<legend>Login - Registrera ny användare - Skriv in användarnnamn och lösenord</legend>
					<label for='UserNameID' >Namn :</label>					
					<input type='text' size='20' name='".self::$USERNAME."' id='UserNameID' value='$user' />
					<br />
					<label for='PasswordID' >Lösenord  :</label>
					<input type='password' size='20' name='".self::$PASSWORD."' id='PasswordID' value='' />
					<br />
					<label for='RepeatPasswordID' >Upprepa lösenord  :</label>
					<input type='password' size='20' name='".self::$REPEATPASSWORD."' id='RepeatPasswordID' value='' />
					<br />
					<label for='RepeatPasswordID' >Skicka  :</label>
					<input id='submitBtn' type='submit' name='".self::$SUBMIT."'  value='Registrera' />
				</fieldset>
			</form>";			
		return $html;
	}
	
	/**
	 * @return boolean
	 */
	public function isRegistrating() {
		return (isset($_GET[self::$REGISTER]) && $this->registstrationOK != true);
	}

	/**
	 * 
	 * @return boolean
	 */
	public function userPosting() {

		return isset($_POST[self::$SUBMIT]);	
	}	
	
	/**
	 * @return String HTML
	 */
	public function getLogoutButton() {

		return "$this->message <a href='?".self::$LOGOUT."'>Logga ut</a>";
	}
	
	/**
	 * Return UserCredentials with posted user data
	 * @return UserCredentials
	 */
	public function getUserCredentials() {
		return \login\model\UserCredentials::createFromClientData(new \login\model\UserName($this->getUserName()), 
														\login\model\Password::fromCleartextRegistrating($this->getPassword(),
														$this->getRepeatedPassword()));		
	}
	
	/**
	 * From \model\RegisterObserver
	 */
	public function registerFailed() {
		$storageError = true;
		
		//Validate input Username
		if(\common\Filter::hasTags($this->getUserName())) {
			$this->message .= "<p>Användarnamnet innehåller ogiltiga tecken.</p>";
			$storageError = false;
		} else if (strlen($this->getUserName()) < 3) {
			$this->message .= "<p>Användarnamnet är för kort. Minst 3 tecken</p>";
			$storageError = false;
		}

		//Validate input Password
		if ($this->getPassword() != $this->getRepeatedPassword()) {
			$this->message .= "<p>Lösenorden matchar inte.</p>";
			$storageError = false;
		} else if (strlen($this->getPassword()) < 6) {
			$this->message .= "<p>Lösenord är för kort. Minst 6 tecken</p>";
			$storageError = false;
		}
		
		//Only happens if there are errors while saving to the datafile
		if ($storageError) {
			$this->message .= "<p>Användarnamnet är redan upptaget.</p>";
		}

	}
	
	/**
	 * From \model\RegisterObserver
	 */
	public function registerOK() {
		$this->LoginView->message  = "<p>Registrering av ny användare lyckades</p>";	
		$this->registstrationOK = true;	
	}
		
	/**
	 * note: private!
	 * @return String
	 */
	private function getUserName() {
		if (isset($_POST[self::$USERNAME]))
			return $_POST[self::$USERNAME];
		else
			return "";
	}	
	
	/**
	 * note: private!
	 * 
	 * @return String
	 */
	private function getPassword() {
		if (isset($_POST[self::$PASSWORD]))
			return \Common\Filter::sanitizeString($_POST[self::$PASSWORD]);
		else
			return "";
	}

	private function getRepeatedPassword() {
		if (isset($_POST[self::$REPEATPASSWORD]))
			return $_POST[self::$REPEATPASSWORD];

		else
			return "";
	}		
}
